package com.xiao.sulingproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiao.sulingproduct.dao.BusinessDao;
import com.xiao.sulingproduct.entity.BusinessEntity;
import com.xiao.sulingproduct.fegin.EsFeign;
import com.xiao.sulingproduct.service.BusinessService;
import com.xiao.sulingproduct.vo.BusinessVo;
import com.xiao.sulingpublic.to.BusinessTo;
import com.xiao.sulingpublic.utils.PageUtils;
import com.xiao.sulingpublic.utils.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Date;
import java.util.Map;


/**
 * @author 13221
 */
@Service("businessService")
public class BusinessServiceImpl extends ServiceImpl<BusinessDao, BusinessEntity> implements BusinessService {

    @Autowired
    private EsFeign esFeign;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BusinessEntity> page = this.page(
                new Query<BusinessEntity>().getPage(params),
                new QueryWrapper<BusinessEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存或修改店铺数据
     * @param businessVo 要保存或修改的数据
     * @return 返回状态码
     */
    @Transactional
    @Override
    public Integer saveOrUp(BusinessVo businessVo) throws IOException {
        //属性对拷
        BusinessEntity businessEntity = new BusinessEntity();
        BeanUtils.copyProperties(businessVo , businessEntity);

        /*创建时间*/
        businessEntity.setBuCreateDate(new Date());
        //保存或修改
        boolean bo = this.saveOrUpdate(businessEntity, new QueryWrapper<BusinessEntity>().eq("bu_member_username", businessEntity.getBuMemberUsername()));

        // 保存/更新 Es中的数据
        BusinessTo businessTo = new BusinessTo();
        BeanUtils.copyProperties(businessEntity,businessTo);
        esFeign.save(businessTo);

        //返回状态
        return bo?1:-1;
    }

    /**
     * 获取店铺信息
     * @param memberUsername :用户号
     * @return 返回店铺信息
     */
    @Override
    public BusinessVo selectByMemberUsername(String memberUsername) {
        BusinessVo data = null;
        BusinessEntity business = baseMapper.selectOne(new QueryWrapper<BusinessEntity>().eq("bu_member_username", memberUsername));
        if(null != business){
            data = new BusinessVo();
            BeanUtils.copyProperties(business,data);
        }
        return data;
    }

}